/*
 * Created by 岱青海蓝信息系统(北京)有限公司 on 17-6-23 上午9:49
 * Copyright (c) 2017. All rights reserved.
 */

package com.example.dqhl.jdmetro.activities;

import android.os.Bundle;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.dqhl.jdmetro.R;
import com.example.dqhl.jdmetro.base.BaseActivity;
import com.example.dqhl.jdmetro.config.Config;
import com.example.dqhl.jdmetro.config.Constant;
import com.example.dqhl.jdmetro.utils.Dlog;
import com.example.dqhl.jdmetro.utils.FormatUtils;
import com.example.dqhl.jdmetro.utils.JsonUtils;
import com.example.dqhl.jdmetro.utils.NetUtils;
import com.example.dqhl.jdmetro.utils.TimeCountRegisterUtil;
import com.example.dqhl.jdmetro.utils.Toastutils;

import org.xutils.common.Callback;
import org.xutils.common.util.MD5;
import org.xutils.http.RequestParams;
import org.xutils.x;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * description:忘记密码
 * autour:guanluocang
 * date:17/6/23 09:50
 */
public class ForgetPasswordActivity extends BaseActivity {


    @Bind(R.id.tv_login)
    TextView tv_login;//进入

    @Bind(R.id.et_userName)
    EditText et_userName;//手机号
    @Bind(R.id.et_editVerify)
    EditText et_editVerify;//验证码
    @Bind(R.id.et_editPasswordConfim)
    EditText et_editPasswordConfim;//确认密码
    @Bind(R.id.et_editPassword)
    EditText et_editPassword;//输入密码a
    @Bind(R.id.tv_sendCode)
    TextView tv_sendCode;//发送验证码
    @Bind(R.id.btn_hidePassword)
    Button btn_hidePassword;//隐藏密码
    @Bind(R.id.btn_hidePasswordConfim)
    Button btn_hidePasswordConfim;//隐藏确认密码

    private long millisInFuture = Constant.TIMECOUNT_REGISTER; //验证码倒计时
    private boolean isHidden = true;
    private boolean isHiddenConfim = true;

    @Override
    public void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_forget_password);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {

    }

    @Override
    public void initListener() {
        tv_login.setOnClickListener(this);
        tv_sendCode.setOnClickListener(this);
        btn_hidePassword.setOnClickListener(this);
        btn_hidePasswordConfim.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_login:
                doForgetPassword();
                break;
            case R.id.tv_sendCode:
                doSendAuthCode();
                break;
            case R.id.btn_hidePassword:
                if (isHidden) {
                    et_editPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    btn_hidePassword.setBackgroundResource(R.drawable.ic_login_show);
                } else {
                    et_editPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    btn_hidePassword.setBackgroundResource(R.drawable.ic_login_hide);
                }
                isHidden = !isHidden;
                et_editPassword.postInvalidate();
                //切换后将EditText光标置于末尾
                CharSequence charSequenceConfim = et_editPassword.getText();
                if (charSequenceConfim instanceof Spannable) {
                    Spannable spanText = (Spannable) charSequenceConfim;
                    Selection.setSelection(spanText, charSequenceConfim.length());
                }
                break;
            case R.id.btn_hidePasswordConfim:
                if (isHiddenConfim) {
                    et_editPasswordConfim.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    btn_hidePasswordConfim.setBackgroundResource(R.drawable.ic_login_show);
                } else {
                    et_editPasswordConfim.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    btn_hidePasswordConfim.setBackgroundResource(R.drawable.ic_login_hide);
                }
                isHiddenConfim = !isHiddenConfim;
                et_editPasswordConfim.postInvalidate();
                //切换后将EditText光标置于末尾
                CharSequence charSequenceConfim_confim = et_editPasswordConfim.getText();
                if (charSequenceConfim_confim instanceof Spannable) {
                    Spannable spanText = (Spannable) charSequenceConfim_confim;
                    Selection.setSelection(spanText, charSequenceConfim_confim.length());
                }
                break;
        }
    }

    /**
     * 忘记密码
     */
    private void doForgetPassword() {
        String usename = et_userName.getText().toString().trim();
        String verify = et_editVerify.getText().toString().trim();
        String password = et_editPassword.getText().toString().trim();
        String passwordConfim = et_editPasswordConfim.getText().toString().trim();

        String passOne = MD5.md5(password);
        String passTwo = MD5.md5(passOne + passOne);
        String passThree = MD5.md5(passTwo + "~!@#$%^&*()_+");

        String passOne_confim = MD5.md5(passwordConfim);
        String passTwo_confim = MD5.md5(passOne_confim + passOne_confim);
        String passThree_confim = MD5.md5(passTwo_confim + "~!@#$%^&*()_+");
        if (TextUtils.isEmpty(usename) || TextUtils.isEmpty(password) || TextUtils.isEmpty(passwordConfim) || TextUtils.isEmpty(verify)) {
            Toastutils.show("请输入");
        } else {
            RequestParams params = new RequestParams(Config.forgetPassword);
            params.addBodyParameter("account", usename);
            params.addBodyParameter("verify", verify);
            params.addBodyParameter("password_a", passThree);
            params.addBodyParameter("password_b", passThree_confim);
            x.http().post(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    Dlog.e("修改密码" + result);
                    int errCode = JsonUtils.getErrCode(result);
                    String errMsg = JsonUtils.getErrMsg(result);
                    if (errCode == 200) {
//                        JumpItent.jump(ForgetPasswordActivity.this, LoginActivity.class);
                        finish();
                    }else{
                        Toastutils.show(errMsg);
                    }
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {

                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {

                }
            });
        }

    }

    /**
     * 发送验证码
     */
    private void doSendAuthCode() {
        final String strTelPhone = et_userName.getText().toString();
        if (!FormatUtils.isMobileNO(strTelPhone)) {
            Toastutils.show("手机格式不正确");
        } else {
            if (NetUtils.isConnected(ForgetPasswordActivity.this)) {
                TimeCountRegisterUtil timeCountUtil = new TimeCountRegisterUtil(this, millisInFuture, 1000, tv_sendCode, R.drawable.bg_register_send_code, R.drawable.bg_register_input_single);
                timeCountUtil.start();
                RequestParams params = new RequestParams(Config.forgetPassword_verify);
                params.addBodyParameter("account", strTelPhone);
                x.http().post(params, new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                        Dlog.e("result:" + result);
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {

                    }

                    @Override
                    public void onCancelled(CancelledException cex) {

                    }

                    @Override
                    public void onFinished() {

                    }
                });
            } else {
                Toastutils.show("网络连接异常");
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
